package com.ruoyi.web.controller.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewPartitions;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.KafkaAdmin;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
@EnableKafka
public class KafkaConfiguration {

    public KafkaConfiguration(KafkaAdmin kafkaAdmin) {
        try {
            Map<String, NewPartitions> newPartitions = new HashMap<>();
            newPartitions.put(KafkaProducer.TOPIC_TEST, NewPartitions.increaseTo(2));
            AdminClient.create(kafkaAdmin.getConfigurationProperties()).createPartitions(newPartitions);
            log.info("设置kafka分区数量成功");
        } catch (Exception ex) {
            log.error("设置kafka分区数量失败：{}", ex.getMessage(), ex);
        }
    }
}
